package cn.hl.hm.service.impl;

import cn.hl.hm.dao.RechargeRecoardMapper;
import cn.hl.hm.entity.RechargeRecoard;
import cn.hl.hm.entity.RechargeRecoardDo;
import cn.hl.hm.service.RechargeRecoardService;
import cn.hl.hm.util.AppPager;
import cn.hl.hm.vo.AccountCheckingVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class RechargeRecoardImpl implements RechargeRecoardService{
	@Autowired
	private RechargeRecoardMapper rechargeRecoardMapper;
	@Override
	public List<RechargeRecoard> selectRechargeRecoard(RechargeRecoard recoard) {
		// TODO Auto-generated method stub
		return rechargeRecoardMapper.selectRechargeRecoard(recoard);
	}

	@Override
	public int addRechargeRecoard(RechargeRecoard recoard) {
		// TODO Auto-generated method stub
		return rechargeRecoardMapper.insertSelective(recoard);
	}

	@Override
	public int updateRechargeRecoard(RechargeRecoard recoard) {
		// TODO Auto-generated method stub
		return rechargeRecoardMapper.updateByPrimaryKeySelective(recoard);
	}

	@Override
	public List<RechargeRecoard> selectRechargeRecoardByUserPhone(RechargeRecoard recoard) {
		// TODO Auto-generated method stub
		return rechargeRecoardMapper.selectRechargeRecoardByUserPhone(recoard);
	}

	@Override
	public List<RechargeRecoard> driverCashDetail(RechargeRecoard recoard) {
		// TODO Auto-generated method stub
		return rechargeRecoardMapper.driverCashDetail(recoard);
	}

	@Override
	public List<RechargeRecoard> selectRechargeRecoardAss(RechargeRecoard recoard) {
		// TODO Auto-generated method stub
		return rechargeRecoardMapper.selectRechargeRecoardAss(recoard);
	}

	@Override
	public int selectRechargeRecoardNum(RechargeRecoard rechargeRecoard) {
		// TODO Auto-generated method stub
		return rechargeRecoardMapper.selectRechargeRecoardNum(rechargeRecoard);
	}

	@Override
	public List<RechargeRecoard> selectSecurityList(RechargeRecoard rechargeRecoard) {
		return rechargeRecoardMapper.selectSecurityList(rechargeRecoard);
	}

	@Override
	public int selectSecurityListNum(RechargeRecoard rechargeRecoard) {
		return rechargeRecoardMapper.selectSecurityListNum(rechargeRecoard);
	}

	@Override
	public BigDecimal selectSumAmount(RechargeRecoard rechargeRecoard) {
		return rechargeRecoardMapper.selectSumAmount(rechargeRecoard);
	}

	@Override
	public List<AccountCheckingVo> selectForAccountChecking() {
		return rechargeRecoardMapper.selectForAccountChecking();
	}

	@Override
	public List<RechargeRecoardDo> selectSecurityAndRoadFeeList(RechargeRecoard rechargeRecoard) {
		return rechargeRecoardMapper.selectSecurityAndRoadFeeList(rechargeRecoard);
	}

	@Override
	public Map getSecurityAndRoadFeeMap(Integer pageId, Integer pageCount, RechargeRecoard rechargeRecoard) {
		try {
			if (StringUtils.isNotBlank(rechargeRecoard.getBeString())
                    && StringUtils.isNotBlank(rechargeRecoard.getEndString())
                    && rechargeRecoard.getEndString().length()>4
                    && rechargeRecoard.getBeString().length()>4) {
				rechargeRecoard.setBeString(rechargeRecoard.getBeString().trim());
				rechargeRecoard.setEndString(rechargeRecoard.getEndString().trim() + " 23:59:59");
            }
			Integer roadfeeCashCount=0;
			BigDecimal roadfeeCash=new BigDecimal(0);
			Integer roadfeeBalanceCount=0;
			BigDecimal roadfeeBalance=new BigDecimal(0);

			Integer securityCashCount=0;
			BigDecimal securityCash=new BigDecimal(0);
			Integer securityBalanceCount=0;
			BigDecimal securityBalance=new BigDecimal(0);
			rechargeRecoard.setPageId(null);
			rechargeRecoard.setPageCount(null);
			List<RechargeRecoardDo> rechargeRecoardListAll =selectSecurityAndRoadFeeList(rechargeRecoard);
			if (rechargeRecoardListAll!=null && rechargeRecoardListAll.size()>0) {
                for (RechargeRecoardDo rechargeRecoardDo : rechargeRecoardListAll) {
                    //保全金
                    if (rechargeRecoardDo.getOperType().equals(5)) {
                        if(rechargeRecoardDo.getSecurityType().equals(2)){//余额
                            securityBalanceCount+=1;
                            securityBalance=securityBalance.add(rechargeRecoardDo.getAmount());
                        }else if(rechargeRecoardDo.getSecurityType().equals(3)){//现金
                            securityCashCount+=1;
                            securityCash=securityCash.add(rechargeRecoardDo.getAmount());
                        }
                    }else{//配载费
						if(rechargeRecoardDo.getPayType().equals(25)){//余额
                            roadfeeBalanceCount+=1;
                            roadfeeBalance=roadfeeBalance.add(rechargeRecoardDo.getPayPrice());
                        }else if(rechargeRecoardDo.getPayType().equals(26)){//现金
							roadfeeCashCount+=1;
							roadfeeCash=roadfeeCash.add(rechargeRecoardDo.getPayPrice());
                        }
                    }
                }
            }
			AppPager handle = AppPager.handle(pageId, pageCount);
			rechargeRecoard.setPageId(handle.getPageId());
			rechargeRecoard.setPageCount(handle.getPageCount());
			List<RechargeRecoardDo> rechargeRecoardList = selectSecurityAndRoadFeeList(rechargeRecoard);

			Map map = AppPager.getPageInfo(rechargeRecoardList, pageCount, rechargeRecoardListAll==null?0:rechargeRecoardListAll.size(),pageId);
			map.put("roadfeeCashCount", roadfeeCashCount);
			map.put("roadfeeCash", roadfeeCash);
			map.put("roadfeeBalanceCount", roadfeeBalanceCount);
			map.put("roadfeeBalance",roadfeeBalance );
			map.put("securityCashCount", securityCashCount);
			map.put("securityCash", securityCash);
			map.put("securityBalanceCount", securityBalanceCount);
			map.put("securityBalance", securityBalance);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
